package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.IndustryType;
import util.enumeration.Language;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;

@FacesConverter(value = "industryTypeConverter", forClass = util.enumeration.IndustryType.class)

public class IndustryTypeConverter implements Converter {
    private WebLocalization localization;
    private String preferredLocale;
    
    public IndustryTypeConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        /* END LOCALIZATION */
    }
    
    //getAsObject method converts a string to a an object
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        System.out.println("enter heres?");
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null || value.length() == 0) {
            return null;
        }
        
        /* NOTE:
         * currently, the checkbox list actually returns all the same string for the object part
         * but, I just split them into diff locales just in case we need them one day.
         */
            
        System.out.println("cuisine converter obj: " + value);
        
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            return getIndustryTypeAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            return getIndustryTypeAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            return getIndustryTypeAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            return getIndustryTypeAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getIndustryTypeAsObject_EN_US(value);
        }
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null) {
            return null;
        }
        
        System.out.println("industry converter str: " + value);
        
        if (value instanceof IndustryType) {
            IndustryType industryType = (IndustryType) value;
            
            String localizedIndustryType = "";
            
            // en_US ENGLISH, UNITED STATES
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
                localizedIndustryType = getIndustryTypeAsString_EN_US(industryType);
            }
            // en_SG ENGLISH, SINGAPORE
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
                localizedIndustryType = getIndustryTypeAsString_EN_SG(industryType);
            }
            // ms_MY MALAY, MALAYSIA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
                localizedIndustryType = getIndustryTypeAsString_MS_MY(industryType);
            }

            // zh_CN CHINESE, CHINA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
                localizedIndustryType = getIndustryTypeAsString_ZH_CN(industryType);
            }
            // other language...
            else {
                localizedIndustryType = getIndustryTypeAsString_EN_US(industryType);
            }

            return localizedIndustryType;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.CuisineType");
        }
    }
    
    // simplified chinese (zh_CN)
    private static Object getIndustryTypeAsObject_ZH_CN(String value){
        if (value.equals("食品与饮料") )return IndustryType.FnB;
        else if(value.equals("酒店业")) return IndustryType.ACCOMMODATION;
        else if(value.equals("旅游景点")) return IndustryType.PLACEOFINTEREST;
        else if(value.equals("活动")) return IndustryType.EVENT;
        return null;
        
    }
    
    private static Object getIndustryTypeAsObject_EN_US(String value){
        if(value.equals("F&B Stakeholder")) return IndustryType.FnB;
        else if(value.equals("Accommodation")) return IndustryType.ACCOMMODATION;
        else if(value.equals("Place of Interest")) return  IndustryType.PLACEOFINTEREST;
        else if(value.equals("Events")) return IndustryType.EVENT;
        return null; 
    }
    
     private static Object getIndustryTypeAsObject_EN_SG(String value){
        if(value.equals("F&B Stakeholder")) return IndustryType.FnB;
        else if(value.equals("Accommodation")) return IndustryType.ACCOMMODATION;
        else if(value.equals("Place of Interest")) return  IndustryType.PLACEOFINTEREST;
        else if(value.equals("Events")) return IndustryType.EVENT;
        return null; 
    }
     // bahasa malaysia (ms_MY)
     private static Object getIndustryTypeAsObject_MS_MY(String value){
         if(value.equals("F & B pihak berkepentingan")) return IndustryType.FnB;
         else if(value.equals("Penginapan")) return IndustryType.ACCOMMODATION;
         else if(value.equals("Tempat-intrerest")) return IndustryType.PLACEOFINTEREST;
         else if(value.equals("Acara")) return IndustryType.EVENT;
         return null; 
     }
    
    
    // simplified chinese (zh_CN)
    private static String getIndustryTypeAsString_ZH_CN(IndustryType industryType) {
        String localizedIndustryType = "";
        
        switch (industryType) {
            case FnB: localizedIndustryType = "食品与饮料" ; break;
            case ACCOMMODATION: localizedIndustryType = "住房"; break;
            case PLACEOFINTEREST: localizedIndustryType = "旅游景点"; break;
            case EVENT: localizedIndustryType = "活动"; break;
        }
        return localizedIndustryType;
    }
    
    // default (en_US)
    private static String getIndustryTypeAsString_EN_US(IndustryType industryType) {
        String localizedIndustryType = "";
        
        switch (industryType) {
            case FnB: localizedIndustryType = "F&B Stakeholder" ; break;
            case ACCOMMODATION: localizedIndustryType = "Accommodation"; break;
            case PLACEOFINTEREST: localizedIndustryType = "Place of Interest"; break;
            case EVENT: localizedIndustryType = "Events"; break;
        }
        return localizedIndustryType;
        
    }
    
    // singapore english (en_SG)
    private static String getIndustryTypeAsString_EN_SG(IndustryType industryType) {
        String localizedIndustryType = "";
        
        switch (industryType) {
            case FnB: localizedIndustryType = "F&B Stakeholder" ; break;
            case ACCOMMODATION: localizedIndustryType = "Accommodation"; break;
            case PLACEOFINTEREST: localizedIndustryType = "Place of Interest"; break;
            case EVENT: localizedIndustryType = "Events"; break;
        }
        return localizedIndustryType;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getIndustryTypeAsString_MS_MY(IndustryType industryType) {
        String localizedIndustryType = "";
        
        switch (industryType) {
            case FnB: localizedIndustryType = "F & B pihak berkepentingan" ; break;
            case ACCOMMODATION: localizedIndustryType = "Penginapan"; break;
            case PLACEOFINTEREST: localizedIndustryType = "Tempat-intrerest"; break;
            case EVENT: localizedIndustryType = "Acara"; break;
        }
        return localizedIndustryType;
    }
}